﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using WindowsInput.Native;
using GamePlugin.Logics.Data;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRunInputBuyPrice : MachineState
    {
        public override void Initialize()
        {
            this.AddState("MoveMouse");
            this["MoveMouse"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.MoveMouse(o, e));

            this.AddState("DeleteAllText");
            this["DeleteAllText"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.DeleteAllText(o, e));

            this.AddState("InputBuyPrice");
            this["InputBuyPrice"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.InputBuyPrice(o, e));

            this.AddState("Finished");
            this["Finished"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.Finished(o, e));

            this.DefaultState = "MoveMouse";
        }

        public void MoveMouse(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(30);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(30);

            Helper.MoveCursor(ActionData.InputBuyPricePoint);
            Helper.Sleep(30);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(30);

            ChangeToState("DeleteAllText");
        }

        public void DeleteAllText(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            for (int j = 0; j < 5; j++)
            {
                Helper.MoveCursor(ActionData.InputBuyPricePoint);
                Helper.Sleep(30);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(30);
                for (int i = 0; i < 6; i++)
                {
                    input.Keyboard.KeyPress(VirtualKeyCode.BACK);
                    Helper.Sleep(30);
                }
            }

            Helper.Sleep(100);

            if (Workspace.Instance.SearchItemCompare.ItemBuyPriceChanged == true ||
                Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.TestAllWithLowValuesMode)
                this.ChangeToState("InputBuyPrice");
            else
                this.ChangeToState("Finished");
        }

        public void InputBuyPrice(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            int BuyPriceInput = Workspace.Instance.SearchItem.ItemBuyPrice;
            if (Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.TestAllWithLowValuesMode)
                BuyPriceInput = Workspace.Instance.SearchOptionData.TestBuyPrice;
            Helper.Sleep(100);
            if (BuyPriceInput > 0)
            {
                System.Windows.Forms.Clipboard.Clear();
                System.Windows.Forms.Clipboard.SetText(BuyPriceInput.ToString());
                Helper.Sleep(100);

                input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
            }
            Helper.Sleep(100);

            ChangeToState("Finished");
        }

        public void Finished(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            this.IsNeedReset = true;
            this.Owner.ChangeToState(Helper.GetPropertyStateName(0));
        }
    }
}
